/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.component.editpart;

import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.tools.DragEditPartsTracker;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Class used to show the shadow of the part when it is moved
 */
public class PortDragTracker extends DragEditPartsTracker{

	/** The port editpart to move */
	protected PortEditPart portPart=null;
	
	/**
	 * Creates a new PortDragTracker
	 * @param portEditPart the port editpart to move
	 */
	public PortDragTracker(PortEditPart portEditPart){
		super(portEditPart);
		this.portPart=portEditPart;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.tools.DragEditPartsTracker#updateTargetRequest()
	 */
	@Override
	protected void updateTargetRequest() {
		ChangeBoundsRequest request = (ChangeBoundsRequest)getTargetRequest();
		GraphNode portg=portPart.getGraphNode();
		super.updateTargetRequest();
		//because of GEF we have 3 types of coordinates
		//XY absolute coordinate
		//relative to the container in the XY coodinates
		//xy coordinate in viewPort
		//get view Port
		Viewport viewport=((FigureCanvas)((ScrollingGraphicalViewer)getCurrentViewer()).getControl()).getViewport();
		Point changementxytoXY=viewport.getViewLocation();
		//absolute position of XY
		Point portXY= LookForElement.calculateAbsolutePosition((GraphNode)(portg), new Point(0,0));
		Point mousexy= request.getLocation();
		//absolute position of the mouse
		Point mouseXY=mousexy.getCopy().translate(changementxytoXY);
		Point absoluteGoodPosition=portPart.inRunnerOfClass(mouseXY.getCopy());
		request.setMoveDelta(absoluteGoodPosition.getCopy().translate(portXY.getCopy().negate()));
	}
}
